package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        //赋值到菜品实体
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //插入数据
        dishMapper.insert(dish);
        //获得返回的菜品id
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            //设置菜味表所属的菜品id
            flavors.forEach(flavor -> {
                flavor.setDishId(id);
            });
            //插入数据表
            dishFlavorsMapper.insert(flavors);
        }

    }

    /**
     * 分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.queryPage(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDishes(List<Long> ids) {
        //起售菜品不能删除
        for (Long id : ids) {
            Dish dish = dishMapper.queryDish(id);
            if (StatusConstant.ENABLE.equals(dish.getStatus())) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //与套餐关联菜品不能删
        List<Long> idList = setmealDishMapper.queryDishId(ids);
        if (idList != null && idList.size() > 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
        dishMapper.deleteDish(ids);
        //删除对应口味数据
        dishFlavorsMapper.deleteDish(ids);
    }

    /**
     * 根据id查找菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO queryById(Long id) {
        Dish dish = dishMapper.queryDish(id);
        List<DishFlavor> dishFlavorList = dishFlavorsMapper.queryById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     */
    @Override
    @Transactional
    public void updateWithFlavors(DishDTO dishDTO) {
        //修改菜品基本信息
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);
        //删除口味表对应信息
        dishFlavorsMapper.deleteDishByOne(dishDTO.getId());
        //添加口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            //设置菜味表所属的菜品id
            flavors.forEach(flavor -> {
                flavor.setDishId(dish.getId());
            });
            //插入数据表
            dishFlavorsMapper.insert(flavors);
        }

    }

    /**
     * 根据分类id查找菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> categoryIdFindDish(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Dish> list = dishMapper.categoryIdFindDish(dish);
        return list;
    }

    /**
     * 菜品起售和停售
     *
     * @param status
     */
    @Override
    @Transactional
    public void onOrOff(Integer status, Long id) {
        //菜品停售
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.updateDish(dish);
        //停售要将当前起售套餐停售
        if (StatusConstant.DISABLE.equals(status)) {
            //通过套餐与菜品关系表获取套餐id
            Long setmealId = setmealDishMapper.getId(id);
            Setmeal setmeal = Setmeal.builder()
                    .status(status)
                    .id(setmealId)
                    .build();
            setmealMapper.update(setmeal);
        }
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.categoryIdFindDish(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.queryById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
